package com.ww.core.activity;

import java.io.InputStream;

import android.content.Intent;
import android.os.Handler;

import com.adchina.android.ads.AdManager;
import com.adchina.android.ads.api.AdFsListener;
import com.adchina.android.ads.api.AdFullScreen;
import com.ww.core.util.DeviceUtil;
import com.ww.core.util.FileUtil;

/**
 * @since 
 * @version 2013-12-19
 * @author wangwei
 */
public class MySplashActivity extends MyActivity implements AdFsListener {
	private AdFullScreen adFs;
	private static boolean mIsLoading = true;
	private  boolean openAdView = true;
	private String FS_ID;
	private Intent intent;



	public void gotoMain(String FS_ID, final String path,final String file,final int db, Intent intent,boolean openAdView) {
		this.FS_ID=FS_ID;
		this.openAdView=openAdView;
		this.intent=intent;
		
		new Thread() {
			public void run() {
				InputStream is = getResources().openRawResource(db);
				FileUtil.copyDbFile(is, path, file);
				handler.sendEmptyMessage(1);
			}
		}.start();
	}

	private void showAdView() {
		if (openAdView && DeviceUtil.checkWIFI(this)) {
			addFsView();
			AdManager.setAnimation(true);
		} else {
			new Thread() {
				public void run() {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					handler.sendEmptyMessage(2);
				}
			}.start();
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			hideProgressDialog();
			switch (msg.what) {
			case 1:
				showAdView();
				break;
			case 2:
				openMainActivity();
				break;
			default:
				break;
			}
		};
	};

	private void addFsView() {
		adFs = new AdFullScreen(this, FS_ID);
		adFs.setAdFsListener(this);
		adFs.start();
		mIsLoading = true;
	}

	@Override
	protected void onDestroy() {
		if (adFs != null)
			adFs.stop();
		super.onDestroy();
	}

	//****************************FS*************************************
	@Override
	public void onReceiveFullScreenAd() {
		if (mIsLoading)
			if (adFs != null)
				adFs.showFs();
	}

	@Override
	public void onDisplayFullScreenAd() {
		//				Toast.makeText(this, "onDisplayFullScreenAd", Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onEndFullScreenLandpage() {
		//			Toast.makeText(this, "onEndFullScreenLandpage", Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onFailedToReceiveFullScreenAd() {
		openMainActivity();

	}

	@Override
	public void onStartFullScreenLandPage() {
		//				Toast.makeText(this, "onStartFullScreenLandPage", Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onClickFullScreenAd() {
		//				Toast.makeText(this, "onClickFullScreenAd", Toast.LENGTH_SHORT).show();

	}

	@Override
	public void onFinishFullScreenAd() {
		openMainActivity();
	}

	private void openMainActivity() {
		startActivity(intent);
		mIsLoading = false;
		finish();
	}

}
